package com.kawa.kemanage.controller.app;

import com.github.pagehelper.PageInfo;
import com.kawa.kemanage.entity.dto.ReadMessageDTO;
import com.kawa.kemanage.entity.SystemMessage;
import com.kawa.kemanage.entity.vo.UnreadCountVO;
import com.kawa.kemanage.exception.BadSqlException;
import com.kawa.kemanage.service.SystemMessageService;
import com.kawa.kemanage.util.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;



/**
 * @Author: KAIyo
 * @Date: 2025/6/20 9:22
 * @Version: v1.0.0
 * @Description: TODO
 **/
@RestController
@RequestMapping("/app/message")
@CrossOrigin
public class SystemMessageController {
    private static final Logger log = LoggerFactory.getLogger(SystemMessageController.class);
    @Autowired
    private SystemMessageService systemMessageService;

    /**
     * 获取个人消息总数
     * @param userId 个人的id
     * @return
     */
    @GetMapping("/unreadCount")
    public Result  getUnreadCount(@RequestParam(name = "userId") Integer userId ) {
        UnreadCountVO unreadCountVO = null;
        try {
            unreadCountVO = systemMessageService.getUnreadCount(userId);
        } catch (BadSqlException e) {
            throw new RuntimeException(e);
        } catch (Exception e){
            log.error("getUnreadCount error: {SystemMessageController running error,method is getUnreadCount}", e.getMessage());
            return Result.error("获取个人消息总数失败：" + e.getMessage());
        }
        return Result.success(unreadCountVO);
    }

    /**
     * 获取个人消息列表
     * @param userId 个人的id
     * @param type 消息类型
     * @param pageNum 页码
     * @param pageSize 每页数量
     * @return
     */
    @GetMapping("/list")
    public Result getSystemMessages(
            @RequestParam(name = "userId" , required = true) Integer userId,
            @RequestParam(name = "type" , required = false) String type,
            @RequestParam(name = "page" , required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(name = "size" , required = false, defaultValue = "10") Integer pageSize){
        PageInfo<SystemMessage> pageInfo = null;
        try {
            pageInfo = systemMessageService.getSystemMessages(userId, type, pageNum, pageSize);
        }catch (Exception e){
            log.error("getSystemMessages error: {SystemMessageController running error,method is getSystemMessages}", e.getMessage());
            return Result.error("获取个人消息列表失败：" + e.getMessage());
        }

        return Result.success(pageInfo);
    }


    @PostMapping("/read")
    public Result readSystemMessage(@RequestBody ReadMessageDTO readMessageDTO){
        try {
            readMessageDTO.setUserId(null);
            systemMessageService.readSystemMessage(readMessageDTO);
        }catch (Exception e){
            log.error("readSystemMessage error: {SystemMessageController running error,method is readSystemMessage}", e.getMessage());
            return Result.error("已读消息失败：" + e.getMessage());
        }
        return Result.success("消息已读成功！！");
    }

    /**
     * 批量已读消息
     * @param readMessageDTO
     * @return
     */
    @PostMapping("/readall")
    public Result readAllSystemMessage(@RequestBody ReadMessageDTO readMessageDTO){
        try {
            readMessageDTO.setMessageId(null);
            if ("null".equals(readMessageDTO.getUserId()) || "".equals(readMessageDTO.getUserId())) {
                log.error("用户id为空");
                return Result.error("请选择要已读的用户");
            }
            systemMessageService.readSystemMessage(readMessageDTO);
        }catch (Exception e){
            log.error("readAllSystemMessage error: {SystemMessageController running error,method is readAllSystemMessage}", e.getMessage());
            return Result.error("已读所有消息失败：" + e.getMessage());
        }
        return Result.success("消息已读成功！！");
    }
}
